package ln.fxspringarchetype.vo;

import javafx.beans.property.*;
import ln.fxspringarchetype.model.Course;
import ln.fxspringarchetype.model.Score;
import ln.fxspringarchetype.model.Student;
import ln.fxspringarchetype.util.DateUtil;

import java.time.LocalDate;

public class ScoreVO extends BaseFxViewModel<Score> {
    /**
     * 记录的id
     */
    private final IntegerProperty recordId = new SimpleIntegerProperty();
    /**
     * 考试日期
     */
    private final ObjectProperty<LocalDate> examDate = new SimpleObjectProperty<>();
    /**
     * 课程得分
     */
    private final DoubleProperty score = new SimpleDoubleProperty();
    /**
     * 当前选中的学生
     */
    private final ObjectProperty<Student> student = new SimpleObjectProperty<>();
    /**
     * 当前选中的课程
     */
    private final ObjectProperty<Course> course = new SimpleObjectProperty<>();

    public Student getStudent() {
        return student.get();
    }

    public void setStudent(Student student) {
        this.student.set(student);
    }

    public ObjectProperty<Student> studentProperty() {
        return student;
    }

    public Course getCourse() {
        return course.get();
    }

    public void setCourse(Course course) {
        this.course.set(course);
    }

    public ObjectProperty<Course> courseProperty() {
        return course;
    }

    public int getRecordId() {
        return recordId.get();
    }

    public void setRecordId(int recordId) {
        this.recordId.set(recordId);
    }

    public IntegerProperty recordIdProperty() {
        return recordId;
    }

    public LocalDate getExamDate() {
        return examDate.get();
    }

    public void setExamDate(LocalDate examDate) {
        this.examDate.set(examDate);
    }

    public ObjectProperty<LocalDate> examDateProperty() {
        return examDate;
    }

    public double getScore() {
        return score.get();
    }

    public void setScore(double score) {
        this.score.set(score);
    }

    public DoubleProperty scoreProperty() {
        return score;
    }

    @Override
    public Score toModel() {
        Score score = new Score();
        score.setId(getRecordId());
        score.setStudent(getStudent());
        score.setScore(getScore());
        score.setCourse(getCourse());
        score.setExamDate(DateUtil.localDateToDate(getExamDate()));
        return score;
    }

    @Override
    public void fromModel(Score score) {
        fromModelSimple(score);
        setStudent(score.getStudent());
        setCourse(score.getCourse());
    }

    /**
     * 不加载学生与课程实体
     */
    public void fromModelSimple(Score score) {
        setRecordId(score.getId());
        setScore(score.getScore());
        setExamDate(DateUtil.dateToLocalDate(score.getExamDate()));
    }
}
